﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Text;

namespace Catering.Catering_Page.richang
{
    public partial class PersonAvgServiceChar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

            string AREAID = UCDDLToolWithYearOfMonth1.GetDDL_Area();
            string CLASSID = UCDDLToolWithYearOfMonth1.GetDDL_Class();
            DateTime DTIME = UCDDLToolWithYearOfMonth1.GetDateTime();
            bool TimeType = UCDDLToolWithYearOfMonth1.GetTimeType() == "Year" ? false : true;
            Dictionary<string, decimal> list = GetReportModel(AREAID, CLASSID);
            View_Monitor vm = new View_Monitor();
            string Title = Bind_Data_to_Controls.Bind_ReportTitle(AREAID, CLASSID, DTIME, "人均服务比例");
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vm.GetPersonAvgService(pid,AREAID, CLASSID, DTIME, TimeType);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string cname = dt.Rows[i]["CName"].ToString();
                    decimal number = 0;
                    decimal catering = 0;
                    if (!(dt.Rows[i]["Number"] is DBNull)) number = Convert.ToDecimal(dt.Rows[i]["Number"]);
                    if (!(dt.Rows[i]["Catering"] is DBNull)) catering = Convert.ToDecimal(dt.Rows[i]["Catering"]);
                    if (number > 0 && catering > 0)
                    {
                        number = number / catering;
                        if (list.ContainsKey(cname)) list[cname] = decimal.Round(number);
                    }
                }
                dt.Dispose();
            }
            string StrX = "";
            string str = "";
            if (list != null)
            {
                StringBuilder Sresult = new StringBuilder("");
                Sresult.Append("{");
                Sresult.Append("name:'人均服务比例',");
                Sresult.Append("data: [");
                string StrData = "";
                foreach (string key in list.Keys)
                {
                    StrX += "'" + key + "',";
                    StrData += list[key] + ",";
                }
                StrData = StrData.Length > 0 ? StrData.Substring(0, StrData.Length - 1) : "";
                Sresult.Append(StrData + "]");
                StrX = StrX.Length > 0 ? StrX.Substring(0, StrX.Length - 1) : "";
                Sresult.Append("}");
                str = Sresult.ToString();
            }
            StrX = "[" + StrX + "]";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myScript", "GetDataGroup('" + Title + "'," + StrX + "," + str + ");", true);
        }

        public Dictionary<int, string> GetClassList(string groupid, string classid)
        {
            Dictionary<int, string> result = new Dictionary<int, string>();
            if (!string.IsNullOrEmpty(groupid) && !groupid.Equals("-10"))
            {
                if (!string.IsNullOrEmpty(classid) && !classid.Equals("-10"))
                {
                    result.Add(Convert.ToInt32(classid), UCDDLToolWithYearOfMonth1.GetAllDDL_Class()[Convert.ToInt32(classid)]);
                    return result;
                }
                else
                {
                    return UCDDLToolWithYearOfMonth1.GetAllDDL_Class();
                }
            }
            else
            {
                Dictionary<int, string> temp = UCDDLToolWithYearOfMonth1.GetAllDDL_Area();
                List<Catering_Model.ClassInfo> listClass = new Catering_Rules.ClassInfo().GetAllList(0);
                if (temp.Count > 0)
                {
                    foreach (int id in temp.Keys)
                    {
                        foreach (Catering_Model.ClassInfo item in listClass.Where(c => c.AreaID == id))
                        {
                            result.Add(item.id, item.CName);
                        }
                    }
                }
            }
            return result;
        }

        public Dictionary<string, decimal> GetReportModel(string groupid, string classid)
        {
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();
            Dictionary<int, string> temp = GetClassList(groupid, classid);
            foreach (string value in temp.Values)
            {
                if (!result.ContainsKey(value)) result.Add(value, 0);
            }
            return result;
        }
    }
}